package ochart.utils
{
	public class DateUtil
	{

		public static function get time():Number
		{
			return new Date().getTime();
		}

		/**
		 *  获取指定时间的Date
		 *
		 *  @param 年
		 *
		 *  @param 月
		 *
		 *  @param 日
		 *
		 *  @param 时
		 *
		 *  @param 分
		 *
		 *  @param 秒
		 *
		 *  @param 毫秒
		 *
		 *  @return 返回指定时间的Date
		 */
		public static function getDate(year:Number = 1970, month:Number = 1, day:Number = 1, hour:Number = 0, minute:Number = 0, second:Number = 0, millisecond:Number = 0):Date
		{
			month = month - 1;

			var date:Date = new Date();
			date.setFullYear(year, month, day);
			date.setHours(hour, minute, second, millisecond);
			return date;
		}

		/**
		 *  将指定的Date按照指定的时间格式转换为字符串输出
		 *
		 *  @param 指定的Date
		 *
		 *  @param 指定的时间格式
		 *
		 *  @return 转换后的时间字符串
		 */
		public static function formatDate(date:Date, formatString:String = "YYYY-MM-DD JJ:NN:SS"):String
		{
			//TODO 等日后实现格式化日期
//			var df:DateFormatter = new DateFormatter();
//			df.formatString = formatString;
//			return df.format(date);
			return null;
		}

		/**
		 *  将当前时间按照指定的时间格式转换为字符串输出
		 *
		 *  @param 指定的时间格式
		 *
		 *  @return 当前时间字符串
		 */
		public static function getNowString(formatString:String = "YYYY-MM-DD JJ:NN:SS"):String
		{
			return DateUtil.formatDate(new Date(), formatString);
		}


		public static function convertFromString(str:String, formatString:String = "YYYY/MM/DD JJ:NN:SS"):Date
		{
			// TODO 将字符串转换为DATE
			switch(formatString)
			{
				case "YYYY/MM/DD JJ:NN:SS":
					var strArray:Array = str.split(" ");

					var dateString:String = strArray[0];
					var timeString:String = strArray[1];

					var dateArray:Array = dateString.split("/");
					var timeArray:Array = timeString.split(":");

					var year:int = int(dateArray[0]);
					var month:int = int(dateArray[1]);
					var day:int = int(dateArray[2]);
					var hour:int = int(timeArray[0]);
					var minute:int = int(timeArray[1]);
					var second:int = int(timeArray[2]);

					return DateUtil.getDate(year, month, day, hour, minute, second);
				default:
					return null;
			}
			return null;
		}

		/*获得两个日期之差 */
		public static function getDateDiff(startDate:Date, endDate:Date):int
		{
			var diff:Number = (Number(endDate) - Number(startDate)) / (3600000 * 24);
			return diff;
		}

		/* 获得现在日期的月份 */
		public static function getMonth():Number
		{
			return new Date().getMonth() + 1;
		}

		public static function getFullYear():Number
		{
			return new Date().getFullYear();
		}

		/* 获得给定年的天数 */
		public static function getDaysOfYear(year:int):int
		{
			var startDate:Date = new Date(year, 0, 1);
			var endDate:Date = new Date(year+1, 0, 1);
			return(getDateDiff(startDate, endDate));
		}

		/* 获得给定年、月的天数 */
		public static function getDaysOfMonth(year:int, month:int):int
		{
			var startDate:Date = new Date(year, month, 1);
			var endDate:Date = new Date(year, month + 1, 1);
			return(getDateDiff(startDate, endDate));

		}

		/* 获得给定年 全年月份的天数 */
		public static function getMonthlyDayCounts(year:int):Array
		{
			var result:Array = [];
			var startDate:Date;
			var endDate:Date;
			var monthlyDayCount:int;
			for(var i:int = 0; i < 12; i++)
			{
				startDate = new Date(year, i, 1);
				endDate = new Date(year, i +1, 1);
				monthlyDayCount = getDateDiff(startDate, endDate);
				result.push(monthlyDayCount);
			}
			return result;
		}

		/* 比较两个日期大小 */
		public static function compare(date1:Date, date2:Date):int
		{
			if(Number(date1) < Number(date2))
			{
				return-1
			}
			if(Number(date1) == Number(date2))
			{
				return 0;
			}
			return 1;
		}

		/* 把date日期转化为String */
		public static function convertToString(date:Date):String
		{
			var year:String = date.getFullYear().toString();
			var month:String = Number(date.getMonth() + 1) < 10 ? "0" + String(date.getMonth() + 1) : String(date.getMonth() + 1);
			var dates:String = Number(date.getDate()) < 10 ? "0" + String(date.getDate()) : String(date.getDate());
			return year + "-" + month + "-" + dates;
		}
	}
}